package com.client.koshercook.server;

import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.gwt.user.client.rpc.IsSerializable;

/** Creating an object with Key of a recipe and its num of elements
 * Calculating the weight is implemented in this class*/
@PersistenceCapable
public class KeyWithNum implements Comparable<KeyWithNum> ,IsSerializable{
    @PrimaryKey
    @Persistent
	private RecipeInfo key;
	
	@Persistent
	private int numOfIngs; //the higher the weight - the better priority to show user. (might be negative)
	
	/** Calculating the recipe weight if user have ings ingredients
	 * @pre ings,recipe != null*/
	public KeyWithNum(RecipeInfo recipe, int numOfIngs) {
		this.key = recipe;
		this.numOfIngs = numOfIngs;
	}
	
	/**get the key of the recipe*/
	public RecipeInfo getKey() {
		return key;
	}
	
	/**get the number of ingredients  in the recipe*/
	public int getNumOfIngs() {
		return numOfIngs;
	}
	
	/**change the number of ingredients in the recipe*/
	public void setNumOfIngs(int num) {
		this.numOfIngs = num;
	}
	
	/**compare function for sorting in decreasing way according to weight
	 * @example Collections.sort(list{4,7,2}) gives list{7,4,2}
	 */
	public int compareTo(KeyWithNum other) {
		if(numOfIngs<other.getNumOfIngs()) return -1;
		if(numOfIngs>other.getNumOfIngs()) return 1;
		return key.getRecipeName().compareTo(other.getKey().getRecipeName());
		}
}
